package view;

import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * This class is responsible for displaying a login screen
 * @author Magnus
 */
public class LoginPanel extends JPanel
{
    private final JLabel loginName, loginPassword, loginImage;
    private final JTextField username, password;
    private final JButton loginButton;

    /**
     * Constructor for class LoginPanel
     */
    public LoginPanel()
    {

        Dimension d = getPreferredSize();
        d.width = 1024;
        d.height = 768;
        setPreferredSize(d);

        //Creating components
        loginImage = new JLabel(new ImageIcon("images\\gotland.gif"));
        loginName = new JLabel("Användarnamn");
        loginPassword = new JLabel("Lösenord");
        username = new JTextField(16);
        password = new JPasswordField(16);
        loginButton = new JButton("Logga in");

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        this.setBackground(Color.WHITE);

        //First row
        c.anchor = GridBagConstraints.SOUTH;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weighty = 0.5;
        add(loginImage, c);

        //Second row
        c.anchor = GridBagConstraints.SOUTH;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weighty = 0.1;
        c.insets = new Insets(0, 0, 12, 330);
        add(loginName, c);

        c.anchor = GridBagConstraints.SOUTH;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weighty = 0.1;
        c.insets = new Insets(0, 0, 12, 0);
        add(username, c);

        //Third row
        c.anchor = GridBagConstraints.NORTH;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weighty = 0.1;
        c.insets = new Insets(0, 0, 0, 300);
        add(loginPassword, c);

        c.anchor = GridBagConstraints.NORTH;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weighty = 0.1;
        c.insets = new Insets(0, 0, 0, 0);
        add(password, c);

        //Fourth row
        c.anchor = GridBagConstraints.NORTH;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weighty = 0.9;
        add(loginButton, c);

        setVisible(true);

    }

    /**
     * Adds an action listener to login button
     * @param listenForLoginButton ActionListener
     */
    public void addLoginPanelListener(ActionListener listenForLoginButton)
    {
        loginButton.addActionListener(listenForLoginButton);
    }

    /**
     * Displays an error message
     */
    public void loginErrorMessage()
    {
        JOptionPane.showMessageDialog(null, "Användarnamnet eller lösenordet är fel, försök igen");
    }

    /**
     * Gets password from input field
     * @return String password
     */
    public String getPassword()
    {
        return password.getText();
    }

    /**
     * Sets the password input field
     * @param passwordField String password
     */
    public void setPasswordField(String passwordField)
    {
        this.password.setText(passwordField);
    }

    /**
     * Gets username from input field
     * @return String username
     */
    public String getUsername()
    {
        return username.getText();
    }

}
